package com.scau.bookstore.service.impl;

import com.scau.bookstore.contract.service.DetailService;
import com.scau.bookstore.common.pojo.BookDetail;
import com.scau.bookstore.repository.mapper.BookDetailDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DetailServiceImpl implements DetailService {

    @Autowired
    private BookDetailDao bookDetailDao;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<BookDetail> findAll() {
        return bookDetailDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<BookDetail> findSearch(Map whereMap, int page, int size) {
        Specification<BookDetail> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return bookDetailDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<BookDetail> findSearch(Map whereMap) {
        Specification<BookDetail> specification = createSpecification(whereMap);
        return bookDetailDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public BookDetail findById(Long id) {
        return bookDetailDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param bookDetail
     */
    public void add(BookDetail bookDetail) {
        bookDetailDao.save(bookDetail);
    }

    /**
     * 修改
     *
     * @param bookDetail
     */
    public void update(BookDetail bookDetail) {
        bookDetailDao.save(bookDetail);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        bookDetailDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<BookDetail> createSpecification(Map searchMap) {

        return (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<Predicate>();
            // 推荐语
            if (searchMap.get("recommendation") != null && !"".equals(searchMap.get("recommendation"))) {
                predicateList.add(cb.like(root.get("recommendation").as(String.class), "%" + (String) searchMap.get("recommendation") + "%"));
            }
            // 内容简介
            if (searchMap.get("content") != null && !"".equals(searchMap.get("content"))) {
                predicateList.add(cb.like(root.get("content").as(String.class), "%" + (String) searchMap.get("content") + "%"));
            }
            // 作者介绍
            if (searchMap.get("authorIntroduction") != null && !"".equals(searchMap.get("authorIntroduction"))) {
                predicateList.add(cb.like(root.get("authorIntroduction").as(String.class), "%" + (String) searchMap.get("authorIntroduction") + "%"));
            }

            return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

        };

    }

}
